<template>
	<div class="text-list"  v-loading.fullscreen.lock="loading"
		element-loading-text="拼命加载中">
		<div class="text-list-nav">
			<h3>资源导航</h3>
			<div class="text-list-nav-box">
				<ul>
					<li class="text-list-nav-path" v-for="item of subjectList" :key="item.subject_id">
						<span :title="item.subject_name">{{item.subject_name}}</span>
						<router-link :to="{path: '/test/item-bank',query: {subject: item.subject_id}}">试题</router-link>
						<router-link :to="{path: '/test/common-library',query: {subject: item.subject_id}}">试卷</router-link>
					</li>
				</ul>
			</div>	
		</div>

		<div class="text-list-cont">
			<div class="text-list-cont-title">
				<span>试卷</span>
				<a @click="routeMore">更多 ></a>
			</div>

			<ul>
				<template v-for="(item, index) of testList">
					<li class="text-list-li text-library" v-if="index < 8"  :key="index">
						<span></span>
						<div class="text-list-li-info">
							<p>{{item.name}}</p>
							<p>更新于：{{item.update_time.substring(0, 16)}}</p>
							<div class="text-list-li-op">
								<ul>
									<li v-if="item.grade">{{item.grade}}</li>
									<li v-if="item.subjec">{{item.subject}}</li>
									<li v-if="item.version_name">{{item.version_name}}</li>
									<li v-if="item.source">{{item.source}}</li>
								</ul>
								<router-link :to="{path: '/test/details', query: {id: item.id}}">预览</router-link>
							</div>
						</div>
					</li>
				</template>
			</ul>

			<div class="dependent" v-if="placeholder">
				<i class="ipng image_state01"></i>
				<p>还没发现任何相关的内容哦~</p>
			</div>
		</div>
	</div>
</template>

<script>
	
	export default {
		data(){
			return {
				testList: [], /*试卷列表*/
				subjectList: [],
				loading: true,
                placeholder: false,
                aa:null,
                cc:null,
			}
		},
		watch: {
			'$store.state.questionChange': {
				handler(val, newval){
					this.getTestList()
				},
				immediate: true
            },
        },
        computed: {
    
            isMulti(){
                return this.$store.getters.isMulti;
            }
        },
		created(){
            this.getTestList()
		},
		mounted(){
            this.$nextTick(()=>{
                setTimeout(()=>{
                    this.multiSubject();
                    this.subjectData()
                },400)
            })
		},
		methods: {
			routeMore(){
				this.$router.push({path: '/test/common-library',query: {subject: sessionStorage.getItem('subjectID')}})
            },
            // 单级-获取学科
			subjectData(){
                if(this.isMulti)return;
                this.subjectList = [];
				let success = (res=>{
					// this.subjectList = res.data;
					let subjectArr = []
					for (let i = 0; i <  res.data.length; i++) {

						if (!subjectArr.includes(res.data[i].subject_id)) {

							this.subjectList.push(res.data[i])
						}

						subjectArr.push( res.data[i].subject_id)
					}

				});

				let params = {
					grade_id: 0,
					subject_id:0,
					version_id:0,
					page_index:0,
					page_size:0
				};

				this.$ajax({
					url: 'api/cate/subject_data',
					method: 'get',
					params: params,
					func: {success: success}
				})
            },
            // 多级-获取学科
            multiSubject(){
                if(this.isMulti){
                    this.subjectList = [];
                    let success = res => {
                        let data = res.data.map(x=>{
                            let obj = {
                                subject_id : x.id,
                                subject_name : x.name
                            }
                            return obj;
                        })
                        let subjectArr = []
                        for (let i = 0; i <  data.length; i++) {
    
                            if (!subjectArr.includes(data[i].subject_id)) {
    
                                this.subjectList.push(data[i])
                            }
    
                            subjectArr.push( data[i].subject_id)
                        }
                    };
    
                    this.$ajax({
                        url: 'api/newlevel/platform_subject',
                        method: 'get',
                        params: {
                            app_code: sessionStorage.getItem('CheckSchoolAppCode'),
                        },
                        func: { success: success }
                    });
                }
            },
			/**
			 * 请求试卷列表
			 * @DateTime 2019-07-22T13:47:02+0800
			 */
			getTestList(){
				let success = ( res=>{
					this.testList = res.data.data;
                    this.loading = false;
                    this.placeholder = this.testList.length != 0 ? false : true;
				});

				let params = {
					page_index: 1,
					page_size: 0,
					status: 1,
					authority: 1,
					subject_id: sessionStorage.getItem('subjectID')
				};

				this.$ajax({
					url: 'api/newtest/test_list',
					method: 'get',
					params: params,
					func: {success: success}
				})
			}
		}
	}
</script>